package com.cxb.view;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.cxb.R;

public class CompassView extends View {

	int width;
	int height;
	int compassWidth;
	int compassHeight;

	Map<String, SoftReference<Bitmap>> imageCache = new HashMap<String, SoftReference<Bitmap>>();
	Bitmap nBitmap;
	Bitmap sBitmap;
	Bitmap eBitmap;
	Bitmap wBitmap;
	Bitmap wnBitmap;
	Bitmap enBitmap;
	Bitmap esBitmap;
	Bitmap wsBitmap;
	Bitmap needle;

	Bitmap currentBitmap = null;

	boolean isShowCompass = false;
	boolean isDrawCompass = false;

	public CompassView(Context context, AttributeSet attrs) {
		super(context, attrs);
		nBitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.n);
		imageCache.put("0", new SoftReference<Bitmap>(nBitmap));

		sBitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.s);
		imageCache.put("180", new SoftReference<Bitmap>(sBitmap));
		wBitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.w);
		imageCache.put("270", new SoftReference<Bitmap>(wBitmap));
		eBitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.e);
		imageCache.put("90", new SoftReference<Bitmap>(eBitmap));
		esBitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.es);
		imageCache.put("135", new SoftReference<Bitmap>(esBitmap));
		wsBitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.ws);
		imageCache.put("225", new SoftReference<Bitmap>(wsBitmap));
		enBitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.ne);
		imageCache.put("45", new SoftReference<Bitmap>(enBitmap));
		wnBitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.wn);
		imageCache.put("315", new SoftReference<Bitmap>(wnBitmap));
		needle = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.needle);
		imageCache.put("needle", new SoftReference<Bitmap>(needle));
		compassWidth = nBitmap.getWidth();
		compassHeight = nBitmap.getHeight();
		currentBitmap = nBitmap;
		this.setBackgroundResource(R.drawable.setting_up_map);
	}

	public void setShowCompass(boolean isShow) {
		isShowCompass = isShow;
		isDrawCompass = false;
		if (isShow) {
			// if (degress >= 0) {
			// this.setBackgroundResource(R.drawable.compass_bg);
			// } else {
			this.setBackgroundResource(R.drawable.gps_locationing);
			// }
		} else {
			this.setBackgroundResource(R.drawable.setting_up_map);
		}
		invalidate();
	}

	private Bitmap getBitmap(String key) {
		Log.i("test", "getBitmap  key=" + key);
		Bitmap bitmap = null;
		try {

			bitmap = imageCache.get(key).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (bitmap != null) {
			return bitmap;
		}

		if ("0".equals(key)) {
			imageCache.put("0", new SoftReference<Bitmap>(nBitmap));
			return nBitmap;
		} else if ("90".equals(key)) {
			imageCache.put("90", new SoftReference<Bitmap>(wBitmap));
			return wBitmap;
		} else if ("180".equals(key)) {
			imageCache.put("90", new SoftReference<Bitmap>(sBitmap));
			return sBitmap;
		} else if ("270".equals(key)) {
			imageCache.put("90", new SoftReference<Bitmap>(eBitmap));
			return eBitmap;
		} else if ("45".equals(key)) {
			imageCache.put("45", new SoftReference<Bitmap>(enBitmap));
			return enBitmap;
		} else if ("135".equals(key)) {
			imageCache.put("135", new SoftReference<Bitmap>(esBitmap));
			return esBitmap;
		} else if ("180".equals(key)) {
			imageCache.put("180", new SoftReference<Bitmap>(sBitmap));
			return sBitmap;
		} else if ("225".equals(key)) {
			imageCache.put("225", new SoftReference<Bitmap>(wsBitmap));
			return wsBitmap;
		} else if ("315".equals(key)) {
			imageCache.put("315", new SoftReference<Bitmap>(wnBitmap));
			return wnBitmap;
		} else if ("needle".equals(key)) {
			imageCache.put("needle", new SoftReference<Bitmap>(needle));
			return needle;
		}
		return null;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		width = this.getWidth();
		height = this.getHeight();
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	// @Override
	// public boolean onTouchEvent(MotionEvent event) {
	// if (event.getAction() == MotionEvent.ACTION_DOWN) {
	// rotateCompass(new Random().nextInt(360));
	// }
	// return super.onTouchEvent(event);
	// }

	int degress = 0;

	public void rotateCompass(float angle) {
		Log.i("test", " angle=" + angle);
		if (angle ==-1) {
			this.setBackgroundResource(R.drawable.gps_locationing);
			// isShowCompass = false;
			isDrawCompass = false;
			return;
		}else if(angle==-2){
			isDrawCompass = true;
			isShowCompass=true;
			return;
		}
		isDrawCompass = true;
//		if (isShowCompass) {
//			this.setBackgroundResource(R.drawable.compass_bg);
//		}
		// isShowCompass = true;
		int rate = (int) Math.round(angle / 45.0);
		int actualdegress = Math.round(rate) * 45;
		degress = actualdegress;
		Log.i("test", " rate=" + rate + " actualdegress=" + actualdegress);

		invalidate();

	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (isShowCompass && isDrawCompass) {
			this.setBackgroundResource(R.drawable.compass_bg);
			int left = (width - compassWidth) / 2;
			int top = (height - compassHeight) / 2;
			currentBitmap = getBitmap(degress % 360 + "");
			canvas.drawBitmap(currentBitmap, left, top, new Paint());

			// canvas.rotate(45);

			Bitmap needleBitmap = getBitmap("needle");
			int centerX = left + compassWidth / 2;
			int centerY = top + compassHeight / 2;
			canvas.rotate(degress, centerX, centerY);
			int needleLeft = centerX - needleBitmap.getWidth() / 2;
			int needleTop = centerY - needleBitmap.getHeight();
			canvas.drawBitmap(needleBitmap, needleLeft, needleTop, new Paint());
		}
		super.onDraw(canvas);
	}
}
